
Table of Contents
Preface .....................................................1
Correcting Misconceptions .......................................1
Organization of the Book ........................................3
Who You Are ................................................8
Versions ...................................................8
Security Issues ...............................................9
Conventions Used in This Book ....................................9
Request for Comments ..........................................11
Acknowledgments .............................................12
 
I: Basic I/O .................................................. 13
1. Introducing I/O ..............................................14
1.1 What Is a Stream? ...........................................14
1.2 Numeric Data .............................................17
1.3 Character Data .............................................20
1.4 Readers and Writers .........................................24
1.5 The Ubiquitous IOException ...................................25
1.6 The Console: System.out, System.in, and System.err ....................26
1.7 Security Checks on I/O .......................................32
 
2. Output Streams .............................................34
2.1 The OutputStream Class ......................................34
2.2 Writing Bytes to Output Streams .................................34
2.3 Writing Arrays of Bytes .......................................36
2.4 Flushing and Closing Output Streams ..............................37
2.5 Subclassing OutputStream .....................................38
2.6 A Graphical User Interface for Output Streams ........................39
 
3. Input Streams ...............................................
3.1 The InputStream Class ........................................42
3.2 The read( ) Method ..........................................42
3.3 Reading Chunks of Data from a Stream ............................42
3.4 Counting the Available Bytes ...................................44
3.5 Skipping Bytes .............................................45
3.6 Closing Input Streams ........................................46
3.7 Marking and Resetting ........................................46
3.8 Subclassing InputStream ......................................47
3.9 An Efficient Stream Copier ....................................47
II: Data Sources ............................................... 50
4. File Streams ................................................51
4.1 Reading Files ..............................................51
4.2 Writing Files ..............................................53
4.3 File Viewer, Part 1 ..........................................56




5. Network Streams ............................................60
5.1 URLs ...................................................60
5.2 URL Connections ...........................................62
5.3 Sockets ..................................................65
5.4 Server Sockets .............................................68
5.5 URLViewer ...............................................71
III: Filter Streams .............................................
6. Filter Streams ...............................................74
6.1 The Filter Stream Classes ......................................75
6.2 The Filter Stream Subclasses ...................................75
6.3 Buffered Streams ...........................................80
6.4 PushbackInputStream ........................................81
6.5 Print Streams ..............................................83
6.6 Multitarget Output Streams .....................................84
6.7 File Viewer, Part 2 ..........................................85
7. Data Streams ...............................................96
7.1 The Data Stream Classes ......................................96
7.2 Reading and Writing Integers ...................................98
7.3 Reading and Writing Floating-Point Numb ers ........................103
7.4 Reading and Writing Booleans ..................................106
7.5 Reading Byte Arrays .........................................106
7.6 Reading and Writing Text .....................................107
7.7 Miscellaneous Methods .......................................111
7.8 Reading and Writing Little-Endian Numb ers .........................111
7.9 Thread Safety .............................................123
7.10 File Viewer, Part 3 .........................................124
 

8. Streams in Memory ...........................................131
8.1 Sequence Input Streams .......................................131
8.2 Byte Array Streams ..........................................132
8.3 Communicating Between Threads with Piped Streams ...................135




9. Compressing Streams .........................................140
9.1 Inflaters and Deflaters ........................................140
9.2 Compressing and Decompressing Streams ...........................152
9.3 Working with Zip Files .......................................159
9.4 Checksums ...............................................172
9.5 JAR Files ................................................176
9.6 File Viewer, Part 4 ..........................................189 10. Cryptographic Streams .......................................193
10.1 Hash Function Basics .......................................193
10.2 The MessageDigest Class .....................................195
10.3 Digest Streams ............................................203
10.4 Encryption Basics ..........................................209
10.5 The Cipher Class ..........................................212
10.6 Cipher Streams ............................................225
10.7 File Viewer, Part 5 .........................................231 IV: Advanced and Miscellaneous Topics .............................. 236
11. Object Serialization ..........................................237
11.1 Reading and Writing Objects ...................................237
11.2 Object Streams ............................................238
11.3 How Object Serialization Wo rks ................................239
11.4 Performance  
11.5 The Serializable Interface .....................................241
11.6 The ObjectInput and ObjectOutput Interfaces ........................247
11.7 Versioning ...............................................249
11.8 Customizing the Serialization Format .............................251
11.9 Resolving Classes ..........................................260
11.10 Resolving Objects .........................................261 11.11 Validation ..............................................
11.12 Sealed Objects ........................................... 261

12. Working with Files ..........................................267
12.1 Understanding Files .........................................267
12.2 Directories and Paths ........................................274
12.3 The File Class ............................................280
12.4 Filename Filters ...........................................299
12.5 File Filters ...............................................300
12.6 File Descriptors ...........................................301
12.7 Random-Access Files .......................................302
12.8 General Techniques for Cross-Platform File Access Code ................304 13. File Dialogs and Choosers ......................................306
13.1 File Dialogs ..............................................306
13.2 JfileChooser ..............................................313
13.3 File Viewer, Part 6 .........................................331
14. Multilingual Character Sets and Unicode ...........................337
14.1 Unicode ................................................337
14.2 Displaying Unicode Text .....................................338
14.3 Unicode Escapes ...........................................345
14.4 UTF-8 .................................................346
14.5 The char Data Type .........................................348
14.6 Other Encodings ...........................................356
14.7 Converting Between Byte Arrays and Strings ........................357 15. Readers and Writers .........................................360
15.1 The java.io.Writer Class ......................................360
15.2 The OutputStreamWriter Class .................................361
15.3 The java.io.Reader Class .....................................363
15.4 The InputStreamReader Class ..................................365
15.5 Character Array Readers and Writers .............................366
15.6 String Readers and Writers ....................................369
15.7 Reading and Writing Files ....................................372
15.8 Buffered Readers and Writers ..................................374
15.9 Print Writers .............................................378
15.10 Piped Readers and Writers ...................................380
15.11 Filtered Readers and Writers ..................................381
15.12 File Viewer Finis ..........................................386 16. Formatted I/O with java.text ....................................395
16.1 The Old Way .............................................395
16.2 Choosing a Locale ..........................................397
16.3 Number Formats ...........................................400
16.4 Specifying Width with FieldPosition ..............................408
16.5 Parsing Input .............................................412
16.6 Decimal Formats ..........................................414
16.7 An Exponential Number Format ................................423 17. The Java Communications API ..................................429
17.1 The Architecture of the Java Communications API ....................429
17.2 Identifying Ports ...........................................430
17.3 Communicating with a Device on a Port ...........................437
17.4 Serial Ports ..............................................443
17.5 Parallel Ports .............................................452
 
V: Appendixes ................................................ 458
A. Additional Resources .........................................459
A.1 Digital Think .............................................459
A.2 Design Patterns ............................................459
A.3 The java.io Package .........................................460
A.4 Network Programming .......................................460
A.5 Data Compression ..........................................461
A.6 Encryption and Related Technology ..............................461
A.7 Object Serialization .........................................462
A.8 International Character Sets and Unicode ...........................462
A.9 Java Communications API .....................................463
A.10 Updates and Breaking News ...................................463
 
B. Character Sets .............................................. 465
Colophon .................................................... 472
